//VueRouter
import VueRouter  from "vue-router";
//引入组件
import HomeIndex from "@/pages/HomeIndex.vue";
import HomeLogin from "@/pages/HomeLogin.vue";
import StudentInfo from "@/pages/StudentInfo.vue";
import ScoreInfo from "@/pages/ScoreInfo.vue";
import AllQuestions from "@/pages/AllQuestions.vue";
import SecondHand from "@/pages/SecondHand.vue";
import CourseInfo from "@/pages/CourseInfo.vue";
//创建并暴露路由器
const router = new VueRouter({
    routes:[
        {
            name:'home',
            path:'/home',
            component:HomeIndex,
            meta:{
                title:'首页',
                isAuth:true
            },
            children:[
                {
                    name:'studentinfo',
                    path:'studentinfo',
                    component:StudentInfo,
                    meta:{
                        title:'学生信息',
                        isAuth:true
                    },
                },
                {
                    name:'scoreinfo',
                    path:'scoreinfo',
                    component:ScoreInfo,
                    meta:{
                        title:'学生成绩',
                        isAuth:true
                    },
                },
                {
                    name:'allquestion',
                    path:'allquestion',
                    component:AllQuestions,
                    meta:{
                        title:'问题列表',
                        isAuth:true
                    },
                },
                {
                    name:'secondhand',
                    path:'secondhand',
                    component:SecondHand,
                    meta:{
                        title:'二手交易',
                        isAuth:true
                    },
                },
                {
                    name:'courseinfo',
                    path:'courseinfo',
                    component:CourseInfo,
                    meta:{
                        title:'课程信息',
                        isAuth:true
                    },
                }
            ]
        },
        {
            name:'login',
            path:'/login',
            component:HomeLogin,
            meta:{
                title:'登录'
            }
        },
        {
            name:'homepage',
            path:'/',
            component:HomeLogin,
            meta:{
                title:'登录'
            }
        }
    ]
})

router.beforeEach((to, from, next)=>{
    if (to.meta.isAuth){
        const token = localStorage.getItem('token');
        if (token){
            next()
        } else {
            next('/login')
        }

    } else {
        next()
    }
})

router.afterEach((to) =>{
    document.title = to.meta.title
})

export default router